﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Reflection;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using Enterprise.Data.Dynamic;

namespace Viewer.BusinessObject.BusinessEntities
{
   
    public partial class NSToChuc //: BusinessBase
    {
        CommonCode common = new CommonCode();
        public List<NSToChuc> GetAll()
        {
            return this.Context.NSToChucs.ToList();
            
        }

        public List<NSToChuc> Search(string queryString)
        {
            if (string.IsNullOrEmpty(queryString)) return this.GetAll();
            return this.Context.NSToChucs.Where(queryString).ToList();
        }

        public IEnumerable LoadDataRootTree()
        {
            return this.Context.NSToChucs.Where(a => a.ToChucLevel.Equals(1) && a.ToChucIDCha.Equals(0)).ToList();
        }

       

        #region Get Field
        public string LayDiaChi(int id)
        {
            DataClassesDataContext db = new DataClassesDataContext();
            var query = (from d in db.CHDiaChis
                         where d.DiaChiID.Equals(id)
                         select d).SingleOrDefault();
            if (query != null)
            {
                string result = query.SoNha + " - " + query.ThonPho + " - " +common.LoadTenHanhChinhById(query.XaPhuongID) + " - " +common.LoadTenHanhChinhById(query.QuanHuyenID)+ " - " +common.LoadTenHanhChinhById(query.TinhTPID);
                return result;
            }
            return string.Empty;

        }

        public string DiaChi
        {
            get
            {
                if (this.CHDiaChi == null) return string.Empty;
                return this.CHDiaChi.SoNha + " - " + this.CHDiaChi.ThonPho + " - " + this.CHDiaChi.XaPhuongID + " - " + this.CHDiaChi.QuanHuyenID;
            }
        }

        public string TenToChucHanhChinhTrucThuoc
        {
            get{
                //if (this.CHToChucHanhChinh == null)
                //{
                //    return string.Empty;
                //}
                //return this.CHToChucHanhChinh.TenToChucHanhChinh;
                return null;
            }
        
        }

        public string TenToChucCha
        {
            get
            {
                if (this.ToChucIDCha == 0) return string.Empty;
                return this.Context.NSToChucs.Where(m => m.ToChucID == this.ToChucIDCha).FirstOrDefault().TenToChucFull;
            }
        }

        private string LayTenCommon(int id)
        {
            var query = this.Context.CHCommonCodes.Where(a => a.CodeID.Equals(id));
            if (query != null && query.ToList().Count > 0)
            {
                return query.Single().CodeValue;
            }
            return string.Empty;
        }
        private string LayTenCha(int IdCha)
        {
            DataClassesDataContext db = new DataClassesDataContext();
            var query = (from d in db.NSToChucs
                         where d.ToChucID.Equals(IdCha)
                         select d).Distinct();
            if (query != null && query.Count() > 0)
            {
                return query.SingleOrDefault().TenToChucFull;
            } return string.Empty;


        }
        public List<NSToChuc> LoadDataNode(int IdCha)
        {
            return this.Context.NSToChucs.Where(a => a.ToChucIDCha.Equals(IdCha)).ToList();
        }
        #endregion
        /// <summary>
        /// Loads the data for grid.
        /// </summary>
        /// <param name="Id">The id.</param>
        /// <returns></returns>
        /// <author>baodt</author>
        /// <datetime>7/1/2011-3:05 PM</datetime>
        public IEnumerable LoadDataForGrid(int Id)
        {
            DataClassesDataContext db = new DataClassesDataContext();
            //if (Id != 0)
            //{
                var query = from tc in db.NSToChucs
                            where tc.ToChucIDCha.Equals(Id)
                            select new
                            {
                                Id = tc.ToChucID,
                                TenToChucFull = tc.TenToChucFull,
                                Tencha = LayTenCha(tc.ToChucIDCha),
                                XepHang = LayTenCommon(tc.XepHangDonViID),
                                KhoiQuanLi = LayTenCommon(tc.KhoiQuanLyID),
                                DiaChi = LayDiaChi(tc.DiaChiID),
                                DiaChiId = tc.DiaChiID,
                                GhiChu = tc.GhiChu
                            };
                return query;
            //}
            //else
            //{

            //}
        }

        public List<int> GetToChucTheoUserID(int pUserID)
        {
            DataClassesDataContext db = new DataClassesDataContext();
            var query = (from tc in db.NSToChucs
                         join nqtc_tc in db.QUNhomQuyenByToChuc_ToChucs on tc.ToChucID equals nqtc_tc.ToChucID
                         join nqtc_nd in db.QUNhomQuyenByToChuc_NguoiDungs on nqtc_tc.NhomQuyenByToChucID equals nqtc_nd.NhomQuyenByToChucID
                         join nqtc in db.QUNhomQuyenByToChucs on nqtc_tc.NhomQuyenByToChucID equals nqtc.NhomQuyenByToChucID
                         where nqtc_nd.NguoiDungID == pUserID
                         select tc.ToChucID).ToList();
            return query;
        }
        public IEnumerable SearchCondition(int id, string ten, int khoi, int xephang, int tochucTrucThuoc)
        {
            var query = from tc in Context.NSToChucs
                        where (id > 0 ? tc.ToChucIDCha.Equals(id) : true)
                        select new
                        {
                            Id = tc.ToChucID,
                            TenToChucFull = tc.TenToChucFull,
                            Tencha = LayTenCha(tc.ToChucIDCha),
                            XepHang = LayTenCommon(tc.XepHangDonViID),
                            KhoiQuanLi = LayTenCommon(tc.KhoiQuanLyID),
                            DiaChi = LayDiaChi(tc.DiaChiID),
                            DiaChiId = tc.DiaChiID,
                            GhiChu = tc.GhiChu,
                            XepHangDonViID = tc.XepHangDonViID,
                            KhoiQuanLyID = tc.KhoiQuanLyID,
                            ToChucIDCha = tc.ToChucIDCha

                        };
            if (!string.IsNullOrEmpty(ten))
            {
                query = query.Where(a => a.TenToChucFull.ToLower().Contains(ten.ToLower()));
            }
            if (khoi > 0)
            {
                query = query.Where(a => a.KhoiQuanLyID.Equals(khoi));
            }
            if (xephang > 0)
            {
                query = query.Where(a => a.XepHangDonViID.Equals(xephang));
            }
            if (tochucTrucThuoc > 0)
            {
                query = query.Where(a => a.ToChucIDCha.Equals(tochucTrucThuoc));
            }

            return query;
        }
        public IEnumerable LoadToChucCha()
        {
            DataClassesDataContext db = new DataClassesDataContext();
            var query = from tc in db.NSToChucs
                        where tc.ToChucLevel.Equals(1)
                        select new
                        {
                            Id = tc.ToChucID,
                            TenToChucFull = tc.TenToChucFull,
                            Tencha = string.Empty,
                            XepHang = LayTenCommon(tc.XepHangDonViID),
                            KhoiQuanLi = LayTenCommon(tc.KhoiQuanLyID),
                            DiaChi = LayDiaChi(tc.DiaChiID),
                            DiaChiId = tc.DiaChiID,
                            GhiChu = tc.GhiChu
                        };
            return query;
        }
        public List<NSToChuc> LoadAllToChuc()
        {
            return this.Context.NSToChucs.ToList();
        }
        public void InsertToChuc(string tenShort, string tenFull, int idToChucCha, int toChucLevel, 
            int xepHangId, int idKhoi, int diachiId, string ghiChu,int donViHanhChinhTrucThuocId,int loaiToChucID)
        {
            using (var context = new DataClassesDataContext())
            {
                NSToChuc item = new NSToChuc();
                item.TenToChucShort = tenShort;
                item.TenToChucFull = tenFull;
                item.ToChucIDCha = idToChucCha;
                item.ToChucLevel = idToChucCha > 0 ? 2 : 1;
                item.XepHangDonViID = xepHangId;
                item.KhoiQuanLyID = idKhoi;
                item.DiaChiID = diachiId;
                item.GhiChu = ghiChu;
                item.DonViHanhChinhTrucThuocID = donViHanhChinhTrucThuocId;
                item.LoaiToChucID = loaiToChucID;
                context.NSToChucs.InsertOnSubmit(item);
                context.SubmitChanges();
            }
        }

        public List<string> LoadToChucById(int Id)
        {
           
            List<string> lst = new List<string>();
            DataClassesDataContext db = new DataClassesDataContext();
            var query = from tc in db.NSToChucs
                        where tc.ToChucID.Equals(Id)
                        select new
                        {
                            Id = tc.ToChucID,
                            TenToChucShort = tc.TenToChucShort,
                            TenToChucFull = tc.TenToChucFull,
                            IdCha = tc.ToChucIDCha,
                            XepHang = tc.XepHangDonViID,
                            KhoiQuanLi = tc.KhoiQuanLyID,
                            DiaChi =  tc.DiaChi,
                            DiaChiId = tc.DiaChiID,
                            GhiChu = tc.GhiChu,
                            TenCha = LayTenCha(tc.ToChucIDCha),
                            TenDonViHanhChinhTrucThuoc=tc.TenToChucHanhChinhTrucThuoc,
                            DonViHanhChinhTrucThuocID = tc.DonViHanhChinhTrucThuocID

                        };
            NSToChuc oNSToChuc = this.Context.NSToChucs.Where(m => m.ToChucID == Id).FirstOrDefault();
            foreach (var item in query)
            {
                lst.Add(oNSToChuc.DiaChi);
                lst.Add(oNSToChuc.DiaChiID.ToString());
                lst.Add(oNSToChuc.GhiChu);
                lst.Add(oNSToChuc.ToChucID.ToString());
                lst.Add(oNSToChuc.KhoiQuanLyID.ToString());
                lst.Add(oNSToChuc.ToChucIDCha.ToString());
                lst.Add(oNSToChuc.TenToChucFull);
                lst.Add(oNSToChuc.TenToChucShort);
                lst.Add(oNSToChuc.XepHangDonViID.ToString());
                lst.Add(oNSToChuc.TenToChucCha.ToString());
                lst.Add(oNSToChuc.TenToChucHanhChinhTrucThuoc.ToString());
                lst.Add(item.DonViHanhChinhTrucThuocID.ToString());

            }
            return lst;
        }
        public void UpdateTochuc(int id, string shortName, string full, int khoi, int xephang, string ghichu, int diachi,int donvihanhchinhtructhuoc,int loaitochuc)
        {
            using (var context = new DataClassesDataContext())
            {
                var item = context.NSToChucs.Single(a => a.ToChucID.Equals(id));
                item.TenToChucShort = shortName;
                item.TenToChucFull = full;
                item.GhiChu = ghichu;
                item.DiaChiID = diachi;
                item.KhoiQuanLyID = khoi;
                item.XepHangDonViID = xephang;
                item.DonViHanhChinhTrucThuocID = donvihanhchinhtructhuoc;
                item.LoaiToChucID = loaitochuc;

                context.SubmitChanges();
            }
        }
        public void DeleteToChuc(int id)
        {
            using (var context = new DataClassesDataContext())
            {
                var item = context.NSToChucs.Single(a => a.ToChucID.Equals(id));
                context.NSToChucs.DeleteOnSubmit(item);
                context.SubmitChanges();
            }
        }
        public string LayTenToChuc(int id)
        {
            var query = this.Context.NSToChucs.Where(a => a.ToChucID.Equals(id));
            if (query != null && query.ToList().Count > 0)
            {
                return query.Single().TenToChucFull;
            }
            return string.Empty;
        }

        public static List<NSToChuc> GetToChucCon(int ToChucID)
        {
            using (DataClassesDataContext Context = new DataClassesDataContext())
            {
                
                return Context.NSToChucs.Where(c => c.ToChucIDCha == ToChucID).ToList();
                
            }
        }

        //public  NSToChuc GetByID(int ToChucID)
        //{
           
        //    return Context.NSToChucs.Where(c => c.ToChucID == ToChucID).FirstOrDefault();
        //}

        public static NSToChuc_Report Report(int pToChucID)
        {

            using (DataClassesDataContext Context = new DataClassesDataContext())
            {
                 return  Context.sp_GetNsToChuc(pToChucID);               
            }
           
        }
    }
}

namespace Viewer.BusinessObject.BusinessEntities
{
    using System.Data.Linq;
    using System.Data.Linq.Mapping;
    using System.Data;
    using System.Collections.Generic;
    using System.Reflection;
    using System.Linq;
    using System.Linq.Expressions;
    using System.ComponentModel;
    using System;
      
    public partial class DataClassesDataContext
    {
        [Function(Name = "dbo.sp_GetNsToChuc")]
        public ISingleResult<NSToChuc_Report> sp_GetNsToChuc2([Parameter(Name = "ToChucID", DbType = "Int")] System.Nullable<int> toChucID)
        {
            IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), toChucID);
            return ((ISingleResult<NSToChuc_Report>)(result.ReturnValue));
        }

        [Function(Name = "dbo.sp_GetNsToChuc")]
        [ResultType(typeof (NSToChuc_Report))]
        public IMultipleResults sp_GetNsToChuc1([Parameter(Name = "ToChucID", DbType = "Int")] System.Nullable<int> toChucID)
        {
            IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), toChucID);
            return ((IMultipleResults)(result.ReturnValue));
        }

        public NSToChuc_Report sp_GetNsToChuc(int pNSToChucID)
        {
            var result = this.ExecuteQuery<NSToChuc_Report>("exec sp_GetNsToChuc @ToChucID={0};",pNSToChucID).Single();
            return result;
        }
    }
}